/* NOTICE
    OpenJaWS - Open Java Weather Station
    
    Copyright (C) 2008 Grant Gardner <grant@lastweekend.com.au>
    
    OpenJaWS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
NOTICE */

package au.com.lastweekend.openjaws.plugins;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.lastweekend.openjaws.api.Readings;
import au.com.lastweekend.openjaws.api.WeatherPlugin;
import au.com.lastweekend.openjaws.api.WeatherPluginException;
import au.com.lastweekend.openjaws.api.WeatherStation;
import au.com.lastweekend.openjaws.api.WeatherStationException;

/**
 * Main plugin that reads the weather data from the station.
 * 
 * It is also responsible for setting the period between reads.
 * 
 * Does nothing if the marker station value is set by {@link HistoricalReadingsPlugin}
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: codetemplates.xml,v 1.2 2005/01/22 06:53:11 ggardner Exp $
 */
public class CurrentReadingsPlugin extends DecoratedWeatherPlugin {

    private static final Logger LOG = LoggerFactory.getLogger(CurrentReadingsPlugin.class);
    private WeatherStation ws;

    private int minDelay = 2;
    private int maxDelay = 10 * 60;

    public CurrentReadingsPlugin(WeatherStation ws, WeatherPlugin decorated) {

        super(decorated);
        this.ws = ws;
    }

    @Override
    protected void preProcess(Readings readings) throws WeatherPluginException {

        if (!HistoricalReadingsPlugin.isHistoricalReading(readings)) {
            fetchCurrent(readings);
        }
    }

    @Override
    protected void postProcess(Readings readings) throws WeatherPluginException {

        if (!HistoricalReadingsPlugin.isHistoricalReading(readings)) {
            readings.setSecondsToNextReading(getDelay());
        }
    }

    protected int getDelay() {

        int secondsToReading = 0;

        try {
            secondsToReading = ws.getReadDelay();
            if (secondsToReading < minDelay) {
                secondsToReading = minDelay;
            }
            if (secondsToReading > maxDelay) {
                secondsToReading = maxDelay;
            }
        } catch (WeatherStationException e1) {
            LOG.warn("Exception fetching read delay", e1);
            secondsToReading = minDelay;
        }
        return secondsToReading;
    }

    private void fetchCurrent(Readings readings) throws WeatherPluginException {

        try {
            readings.setReadingTime(new Date());
            ws.fetchReadings(readings);
            LOG.info(String.format("Fetched readings @ %tc", readings.getReadingTime()));
        } catch (WeatherStationException e1) {
            throw new WeatherPluginException("Exception fetching current readings", e1);
        }

    }

    public int getMinDelay() {

        return minDelay;
    }

    /**
     * 
     * @param minDelay
     *            between readings in seconds
     */
    public void setMinDelay(int minDelay) {

        this.minDelay = minDelay;
    }

    public int getMaxDelay() {

        return maxDelay;
    }

    /**
     * 
     * @param maxDelay
     *            between readings in seconds
     */
    public void setMaxDelay(int maxDelay) {

        this.maxDelay = maxDelay;
    }

}
